﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Net.Security;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Timers;
using System.Windows.Forms;
using System.Xml;
using JCore.LoLChat;
using JCore.LoLChat.Classes;
using JCore.LoLProfiler_Test.Classes.Objects;
using bedrock;
using jabber.client;
using jabber.protocol;
using jabber.protocol.client;
using jabber.protocol.iq;
using Friend = JCore.LoLProfiler_Test.Classes.Objects.Friend;
using Match = System.Text.RegularExpressions.Match;

namespace JCore.LoLProfiler_Test.Classes.API
{
	class GetFriendList
	{
		private readonly JabberClient jabberClient = new JabberClient();
		private readonly RosterManager rosterManager = new RosterManager();
		
		public List<Friend> FriendsList = new List<Friend>();
		// public List<> 
		// public BindingList<Class2> bindingList = new BindingList<Class2>();
		// public Dictionary<string, Class2> dictionary = new Dictionary<string, Class2>();

		public void Connect(string username, string password, ISynchronizeInvoke si, int server = 1)
		{
			/*
			 * server can be 0, 1 or 2
			 * 0: NA
			 * 1: EUW
			 * 2: EUEA
			*/
			// Connecting
			try
			{
				AddEventHandlers();
				jabberClient.InvokeControl = si;
				jabberClient.User = username;
				jabberClient.Password = "AIR_jaapjan95";
				// jabberClient.Password = "AIR_" + encryptString(secureString(password));
				jabberClient.Port = 5223;
				jabberClient.SSL = true;
				jabberClient.AutoRoster = true;
				jabberClient.AutoLogin = true;
				jabberClient.AutoPresence = true;
				jabberClient.NetworkHost = "chat.eu.lol.riotgames.com";
				jabberClient.Server = "pvp.net";
				rosterManager.Stream = jabberClient;
				jabberClient.Connect();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void AddEventHandlers()
		{
			rosterManager.OnRosterItem += new RosterItemHandler(OnRosterItem);
			rosterManager.OnRosterEnd += new ObjectHandler(OnRosterEnd);
            // Certificate Check: not usefull
            jabberClient.OnInvalidCertificate += new RemoteCertificateValidationCallback(certificateCallback);
		}

        private bool certificateCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }

		private void OnRosterItem(object sender, Item item)
		{
            Friend friend = new Friend();
			friend.Name = item.Nickname;
		    friend.SummonerId = Int32.Parse(item.JID.User.Replace("sum", ""), NumberStyles.Integer);
			FriendsList.Add(friend);
		}

		private void OnRosterEnd(object sender)
		{
			frmGetInfoTest.getInstance().CreateDatabase(FriendsList);
		}
	}
}
